package com.mishitu.cf.androidfire.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.mishitu.cf.androidfire.R;
import com.mishitu.cf.androidfire.api.ApiConstants;
import com.mishitu.cf.androidfire.api.ApiService;
import com.mishitu.cf.androidfire.fragments.BlankFragment;
import com.mishitu.cf.androidfire.bean.NewsBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.util.Log.e;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View id = findViewById(R.id.fragment);
        BlankFragment blankFragment = new BlankFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment, blankFragment).commit();
        rxjava();
    }

    ApiService apiService;

    private void rxjava() {

        Retrofit build = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ApiConstants.NETEAST_HOST)
                .build();
        ApiService apiService = build.create(ApiService.class);
        this.apiService = apiService;
        retrofit2.Call<NewsBean> news = apiService.getNews(ApiConstants.HEADLINE_TYPE,ApiConstants.CAR_ID,1);
        news.enqueue(new retrofit2.Callback<NewsBean>() {
            @Override
            public void onResponse(retrofit2.Call<NewsBean> call, retrofit2.Response<NewsBean> response) {
                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                Log.e("","");
            }

            @Override
            public void onFailure(retrofit2.Call<NewsBean> call, Throwable t) {
                Log.e("","");
            }
        });
        Log.e("TAG", Thread.currentThread().getName());
//        Subscription subscribe = Observable.create(new Observable.OnSubscribe<NewsBean>() {
//            @Override
//            public void call(Subscriber<? super NewsBean> subscriber) {
//
//                subscriber.onNext(apiService.getNews());
//                subscriber.onCompleted();
//            }
//        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<NewsBean>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                Log.e("TAG", "onError");
//            }
//
//            @Override
//            public void onNext(NewsBean newsBean) {
//                Log.e("TAG", Thread.currentThread().getName());
//                Toast.makeText(MainActivity.this, newsBean.toString(), Toast.LENGTH_SHORT).show();
//            }
//
//        });
    }

    public void get() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder url = new Request.Builder().url("http://blog.csdn.net/tangxl2008008/article/details/51730187");
        Request request = url.build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e("TAG", "onFailure");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                e("TAG", "onResponse");
            }
        });
    }
}
